(function() {
  var express, iced, moment, pgsql, router, __iced_k, __iced_k_noop;

  iced = require('iced-runtime');
  __iced_k = __iced_k_noop = function() {};

  express = require('express');

  pgsql = require('pg');

  moment = require('moment');

  router = express.Router();

  module.exports = router;

  router.get('/', function(req, res, next) {
    var client, done, err, result, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/index.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 11
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/index.coffee"
          });
          client.query("select count(*) as count from pay_hosts", __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 16
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          res.locals.n_hosts = result.rows[0].count;
          (function(__iced_k) {
            __iced_deferrals = new iced.Deferrals(__iced_k, {
              parent: ___iced_passed_deferral,
              filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/index.coffee"
            });
            client.query("select count(uuid) as count from (select distinct uuid from pay_trlist where sys_time > CURRENT_DATE) as temp", __iced_deferrals.defer({
              assign_fn: (function() {
                return function() {
                  err = arguments[0];
                  return result = arguments[1];
                };
              })(),
              lineno: 24
            }));
            __iced_deferrals._fulfill();
          })(function() {
            if (err) {
              done(client);
              return next(err);
            }
            res.locals.total_cnt = result.rows[0].count;
            (function(__iced_k) {
              __iced_deferrals = new iced.Deferrals(__iced_k, {
                parent: ___iced_passed_deferral,
                filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/index.coffee"
              });
              client.query("select count(uuid) as count from (select distinct uuid from pay_trlist where sys_time > CURRENT_DATE and status = 1) as temp", __iced_deferrals.defer({
                assign_fn: (function() {
                  return function() {
                    err = arguments[0];
                    return result = arguments[1];
                  };
                })(),
                lineno: 33
              }));
              __iced_deferrals._fulfill();
            })(function() {
              if (err) {
                done(client);
                return next(err);
              }
              res.locals.abnormay_cnt = result.rows[0].count;
              (function(__iced_k) {
                __iced_deferrals = new iced.Deferrals(__iced_k, {
                  parent: ___iced_passed_deferral,
                  filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/index.coffee"
                });
                client.query("select count(uuid) as count from (select distinct uuid from pay_trlist where sys_time > CURRENT_DATE and chnl = 1 and json_extract_path_text(o_pkt, '39') <> '00') as temp", __iced_deferrals.defer({
                  assign_fn: (function() {
                    return function() {
                      err = arguments[0];
                      return result = arguments[1];
                    };
                  })(),
                  lineno: 44
                }));
                __iced_deferrals._fulfill();
              })(function() {
                if (err) {
                  done(client);
                  return next(err);
                }
                res.locals.bad_cnt = result.rows[0].count;
                (function(__iced_k) {
                  __iced_deferrals = new iced.Deferrals(__iced_k, {
                    parent: ___iced_passed_deferral,
                    filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/index.coffee"
                  });
                  client.query("select count(uuid) as count from (select distinct uuid from pay_trlist where sys_time > CURRENT_DATE and json_extract_path_text(trinfo, 'settl') = 'true') as temp", __iced_deferrals.defer({
                    assign_fn: (function() {
                      return function() {
                        err = arguments[0];
                        return result = arguments[1];
                      };
                    })(),
                    lineno: 54
                  }));
                  __iced_deferrals._fulfill();
                })(function() {
                  if (err) {
                    done(client);
                    return next(err);
                  }
                  res.locals.settl_cnt = result.rows[0].count;
                  res.locals.settl_rate = ((res.locals.settl_cnt * 100) / res.locals.total_cnt).toFixed(2);
                  (function(__iced_k) {
                    __iced_deferrals = new iced.Deferrals(__iced_k, {
                      parent: ___iced_passed_deferral,
                      filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/index.coffee"
                    });
                    client.query("select sys_time from pay_trlist where sys_time > CURRENT_DATE order by sys_time limit 1", __iced_deferrals.defer({
                      assign_fn: (function() {
                        return function() {
                          err = arguments[0];
                          return result = arguments[1];
                        };
                      })(),
                      lineno: 64
                    }));
                    __iced_deferrals._fulfill();
                  })(function() {
                    if (err) {
                      done(client);
                      return next(err);
                    }
                    if (result.rows.length > 0) {
                      res.locals.first_trans_time = result.rows[0].sys_time.format("HH 点 mm 分");
                    } else {
                      res.locals.first_trans_time = 'NaN';
                    }
                    (function(__iced_k) {
                      __iced_deferrals = new iced.Deferrals(__iced_k, {
                        parent: ___iced_passed_deferral,
                        filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/index.coffee"
                      });
                      client.query("select date_trunc('minute', sys_time) as minute, count(distinct uuid) as count from pay_trlist where sys_time > CURRENT_DATE group by minute order by count desc limit 1", __iced_deferrals.defer({
                        assign_fn: (function() {
                          return function() {
                            err = arguments[0];
                            return result = arguments[1];
                          };
                        })(),
                        lineno: 77
                      }));
                      __iced_deferrals._fulfill();
                    })(function() {
                      if (err) {
                        done(client);
                        return next(err);
                      }
                      if (result.rows.length > 0) {
                        res.locals.max_trans_cnt = result.rows[0].count;
                        res.locals.max_trans_time = result.rows[0].minute.format('HH 点 mm 分');
                      } else {
                        res.locals.max_trans_cnt = 0;
                        res.locals.max_trans_time = 'NaN';
                      }
                      done();
                      return res.render('index');
                    });
                  });
                });
              });
            });
          });
        });
      };
    })(this));
  });

  router.post('/index/rundata', function(req, res, next) {
    var all_data, client, done, err, error_data, found, i, item, item2, j, record, result, settl_data, time, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    res.type('json');
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/index.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 92
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/index.coffee"
          });
          client.query("select date_trunc('minute', sys_time) as minute, count(distinct uuid) as count from pay_trlist where sys_time > CURRENT_DATE group by minute order by minute", __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 101
          }));
          __iced_deferrals._fulfill();
        })(function() {
          var _i, _len, _ref;
          if (err) {
            done(client);
            return next(err);
          }
          all_data = [];
          _ref = result.rows;
          for (_i = 0, _len = _ref.length; _i < _len; _i++) {
            record = _ref[_i];
            all_data.push({
              value: [record.minute.format('YYYY-MM-DD HH:mm:ss'), record.count]
            });
          }
          (function(__iced_k) {
            __iced_deferrals = new iced.Deferrals(__iced_k, {
              parent: ___iced_passed_deferral,
              filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/index.coffee"
            });
            client.query("select date_trunc('minute', sys_time) as minute, count(distinct uuid) as count from pay_trlist where sys_time > CURRENT_DATE and json_extract_path_text(trinfo, 'settl') = 'true' group by minute order by minute", __iced_deferrals.defer({
              assign_fn: (function() {
                return function() {
                  err = arguments[0];
                  return result = arguments[1];
                };
              })(),
              lineno: 117
            }));
            __iced_deferrals._fulfill();
          })(function() {
            var _j, _len1, _ref1;
            if (err) {
              done(client);
              return next(err);
            }
            settl_data = [];
            _ref1 = result.rows;
            for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
              record = _ref1[_j];
              settl_data.push({
                value: [record.minute.format('YYYY-MM-DD HH:mm:ss'), record.count]
              });
            }
            (function(__iced_k) {
              __iced_deferrals = new iced.Deferrals(__iced_k, {
                parent: ___iced_passed_deferral,
                filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/index.coffee"
              });
              client.query("select date_trunc('minute', sys_time) as minute, count(distinct uuid) as count from pay_trlist where sys_time > CURRENT_DATE and json_extract_path_text(trinfo, 'settl') = 'true' and (status = 1 or case chnl when 1 then json_extract_path_text(o_pkt, '39') <> '00' when 2 then json_extract_path_text(o_pkt, '39') <> '00' when 3 then json_extract_path_text(o_pkt, 'respCode') <> '00' end) group by minute order by minute", __iced_deferrals.defer({
                assign_fn: (function() {
                  return function() {
                    err = arguments[0];
                    return result = arguments[1];
                  };
                })(),
                lineno: 139
              }));
              __iced_deferrals._fulfill();
            })(function() {
              var _k, _l, _len2, _len3, _len4, _len5, _len6, _m, _n, _o, _ref2;
              if (err) {
                done(client);
                return next(err);
              }
              done();
              error_data = [];
              _ref2 = result.rows;
              for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) {
                record = _ref2[_k];
                error_data.push({
                  value: [record.minute.format('YYYY-MM-DD HH:mm:ss'), record.count]
                });
              }
              if (all_data.length < 2) {
                all_data.push({
                  value: [moment().format('YYYY-MM-DD 00:00:01'), 0]
                });
              }
              if (all_data.length < 2) {
                all_data.push({
                  value: [moment().format('YYYY-MM-DD HH:mm:ss'), 0]
                });
              }
              for (i = _l = 0, _len3 = all_data.length; _l < _len3; i = ++_l) {
                item = all_data[i];
                time = item.value[0];
                found = false;
                for (j = _m = 0, _len4 = settl_data.length; _m < _len4; j = ++_m) {
                  item2 = settl_data[j];
                  if (item2.value[0] === time) {
                    found = true;
                    break;
                  }
                }
                if (!found) {
                  settl_data.push({
                    value: [time, 0]
                  });
                }
              }
              settl_data.sort(function(a, b) {
                if (a.value[0] > b.value[0]) {
                  return 1;
                }
                if (a.value[0] < b.value[0]) {
                  return -1;
                }
                return 0;
              });
              for (i = _n = 0, _len5 = all_data.length; _n < _len5; i = ++_n) {
                item = all_data[i];
                time = item.value[0];
                found = false;
                for (j = _o = 0, _len6 = error_data.length; _o < _len6; j = ++_o) {
                  item2 = error_data[j];
                  if (item2.value[0] === time) {
                    found = true;
                    break;
                  }
                }
                if (!found) {
                  error_data.push({
                    value: [time, 0]
                  });
                }
              }
              error_data.sort(function(a, b) {
                if (a.value[0] > b.value[0]) {
                  return 1;
                }
                if (a.value[0] < b.value[0]) {
                  return -1;
                }
                return 0;
              });
              return res.json({
                succ: true,
                all_data: all_data,
                settl_data: settl_data,
                error_data: error_data
              });
            });
          });
        });
      };
    })(this));
  });

}).call(this);
